define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/shoplist/index' + location.search,
                    add_url: 'shop/shoplist/add',
                    edit_url: 'shop/shoplist/edit',
                    del_url: 'shop/shoplist/del',
                    multi_url: 'shop/shoplist/multi',
                    table: 'shoplist',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title')},
                        {field: 'subtitle', title: __('Subtitle')},
                        {field: 'category_id', title: __('Category_id')},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'original_price', title: __('Original_price'), operate:'BETWEEN'},
                        {field: 'price_title', title: __('Price_title')},
                        {field: 'smallimages', title: __('Smallimages'), events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'shoplike', title: __('Shoplike')},
                        {field: 'recommend', title: __('Recommend')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'spell_status', title: __('Spell_status'), searchList: {"0":__('Spell_status 0'),"1":__('Spell_status 1')}, formatter: Table.api.formatter.status},
                        {field: 'spell_num', title: __('Spell_num')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'shop_id', title: __('Shop_id')},
                        {field: 'setmeal_ids', title: __('Setmeal_ids')},
                        {field: 'manystoretaps_ids', title: __('Manystoretaps_ids')},
                        {field: 'attachfile', title: __('Attachfile')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-setmeal_ids").data("params", function (obj) {
                return {custom: {shop_id: $("#c-shop_id").val()}};
            });
            $("#c-manystoretaps_ids").data("params", function (obj) {
                return {custom: {shop_id: $("#c-shop_id").val()}};
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-setmeal_ids").data("params", function (obj) {
                return {custom: {shop_id: $("#c-shop_id").val()}};
            });
            $("#c-manystoretaps_ids").data("params", function (obj) {
                return {custom: {shop_id: $("#c-shop_id").val()}};
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
